package hb.software.salary.entitygenerator;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

import hb.software.salary.domain.Salary;
import hb.software.salary.service.SalaryManager;
import hb.software.salary.service.SalaryManagerTest;

/**
 * @ClassName: SalaryEntityGenerator
 * @Description: Import Excel contents into the database
 * @author LiXiangxiang
 * @date May 27, 2020 6:55:00 PM
 * 
 */
@ContextConfiguration(locations = { "classpath:applicationContextTest-resources.xml",
		"classpath:applicationContext-dao.xml", "classpath:applicationContext-service.xml" })
public class SalaryEntityGenerator extends AbstractJUnit4SpringContextTests {

	List<Salary> salaryList;

	SalaryManager salaryManager;

	@Autowired
	public void setSalaryManager(SalaryManager salaryManager) {
		this.salaryManager = salaryManager;
	}

	@Before
	public void setUp() throws Exception {
		this.salaryList = new ArrayList<Salary>();
		InputStream input = SalaryManagerTest.class.getResourceAsStream("/workermanagerment_salary_software.xlsx");
		@SuppressWarnings("resource")
		XSSFWorkbook wb = new XSSFWorkbook(input);
		XSSFSheet sheet = wb.getSheetAt(0);
		DataFormatter dataFormatter = new DataFormatter();
		for (int i = 1; i <= sheet.getLastRowNum(); i++) {
			Row row = sheet.getRow(i);
			Salary s = new Salary();
			for (int j = 0; j < row.getLastCellNum(); j++) {
				Cell cell = row.getCell(j);
				if (cell != null) {
					String value = dataFormatter.formatCellValue(cell);
					if (j == 1)
						s.setName(value);
					if (j == 2)
						s.setBasicsalary(Double.valueOf(value).doubleValue());
					if (j == 3)
						s.setAbsence(Integer.valueOf(value).intValue());
					if (j == 4)
						s.setVariablesalary(Double.valueOf(value).doubleValue());
					if (j == 5)
						s.setSalary(Double.valueOf(value).doubleValue());
					if (j == 6)
						s.setWageday(value);
				}
			}
			this.salaryList.add(s);
		}
	}

	@Test
	public void test() {
		this.salaryManager.deleteAll();
		this.salaryManager.save(this.salaryList);
		Iterable<Salary> result = this.salaryManager.findAll();
		if (logger.isInfoEnabled()) {
			logger.info("test() - List<Student> result=" + result); //$NON-NLS-1$
		}
	}

}
